package com.mp.api;

import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.mp.core.utils.HttpClientUtil;
import com.mp.dao.UserMapper;
import com.mp.pojo.User;
import com.mp.service.UserService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * @author jack
 * @data 2019-08-08 17:28
 */
@RestController
public class TestApi {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;


    private static Logger logger = LoggerFactory.getLogger(TestApi.class);

    @Test
    public void deleteById() {
        boolean flag = userService.removeById(1158640291231752193L);
        System.out.println(flag);

    }

    @RequestMapping("/hello")
    @Transactional
    public Object hello() throws InterruptedException {

//        boolean flag = new LambdaUpdateChainWrapper<>(userMapper).eq(User::getId, 1159354363665436675L).set(User::getAge, 40).update();
//        Thread.sleep(5000);
//        System.out.println(flag);
        logger.error("test error",  new RuntimeException("lala"));
        logger.info("test error");
        logger.warn("test error");
        return "ok";
    }

    @RequestMapping("/hello2")
    @Transactional
    public Object hello2() throws InterruptedException {
        boolean flag = new LambdaUpdateChainWrapper<>(userMapper).eq(User::getId, 1159354363665436676L).set(User::getAge, 12).update();
        System.out.println(flag);
        return flag;
    }

    public static void main(String[] args) throws InterruptedException {
        HashMap<String, String> getPhoneMap = new HashMap<>();
        getPhoneMap.put("action", "getPhone");
        getPhoneMap.put("password", "Qwqw12121");
        getPhoneMap.put("sid", "1328");

        String getPhone = HttpClientUtil.doGet("http://www.ximahuang.com/alz/api", getPhoneMap);
        String[] getPhoneSplit = getPhone.split("\\|");
        if (getPhoneSplit[0].equals("1")) {
            System.out.println("获取到的手机号为: " + getPhoneSplit[1]);
            HashMap<String, String> getMessageMap = new HashMap<>();
            getMessageMap.put("action", "getMessage");
            getMessageMap.put("token", "d0b36ee2c1cd1ec59f5579ca6ba832df");
            getMessageMap.put("phone", getPhoneSplit[1]);
            getMessageMap.put("sid", "1328");

            Integer n = 1;
            while (true) {
                String getMessage = HttpClientUtil.doGet("http://www.ximahuang.com/alz/api", getMessageMap);
                String[] getMessageSplit = getMessage.split("\\|");
                if (getMessageSplit[0].equals("0")){
                    System.out.println("手机号：" + getPhoneSplit[1] +  " 未获取到验证码，第" + n + "次 睡眠3秒,原因: " + getMessageSplit[1]);
                    //未获取到验证码 睡眠3秒
                    Thread.sleep(3 * 1000);
                }else {
                    System.out.println("获取到验证码: " + getMessageSplit[1]);
                    return;
                }
                //如果一超过60次取不到短信，可以加黑该手机号
                if (n.equals(60)){

                    HashMap<String, String> addBlackListMap = new HashMap<>();
                    addBlackListMap.put("action", "addBlacklist");
                    addBlackListMap.put("token", "d0b36ee2c1cd1ec59f5579ca6ba832df");
                    addBlackListMap.put("phone", getPhoneSplit[1]);
                    addBlackListMap.put("sid", "1328");
                    String addBlackList = HttpClientUtil.doGet("http://www.ximahuang.com/alz/api", addBlackListMap);
                    System.out.println(addBlackList);
                    return;
                }
                n ++;
            }

        } else {
            System.out.println("获取手机号失败: " + getPhoneSplit[1]);

        }


    }




}
